/**
 * @filename		AlarmManagerUtil.java
 * @author			dhy
 * @date				2016年7月06日-上午10:27:09
 * @description	TODO
 */
package com.android.functionmanagement.util;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.android.functionmanagement.globle.ConstantGloble;
import java.util.Calendar;

/**
 * @author dhy
 * 
 */

public class AlarmManagerUtil {

	/**
	 * 
	 * @param context
	 * @param timeInMillis
	 * @param intent
	 */
	public static void setAlarmTime(Context context, long timeInMillis,
			Intent intent) {
		AlarmManager am = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		int type = intent.getIntExtra("COM_TYPE", -1);
		PendingIntent sender = null;
		switch (type) {
		case 0:
			sender = PendingIntent.getActivity(context,
					intent.getIntExtra(ConstantGloble.REQUEST_CODE, 0), intent,
					PendingIntent.FLAG_CANCEL_CURRENT);
			break;
		case 1:
			sender = PendingIntent.getBroadcast(context,
					intent.getIntExtra(ConstantGloble.REQUEST_CODE, 0), intent,
					PendingIntent.FLAG_CANCEL_CURRENT);
			break;
		case 2:
			sender = PendingIntent.getService(context,
					intent.getIntExtra(ConstantGloble.REQUEST_CODE, 0), intent,
					PendingIntent.FLAG_CANCEL_CURRENT);
			break;
		default:
			break;
		}
		if (sender == null)
			return;
		int interval = (int) intent.getLongExtra(
				ConstantGloble.INTERVAL_MILLIS, 0);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			am.setWindow(AlarmManager.RTC_WAKEUP, timeInMillis, interval,
					sender);
		}
	}

	/**
	 * 
	 * @param context
	 * @param action
	 * @param requestCode
	 *            闹钟的id
	 * @param getWhat
	 *            0表示Activity，1表示Broadcast，2表示Service
	 * @param cl
	 *            class .三大组件之一
	 */
	public static void cancelAlarm(Context context, int action,
			int requestCode, int getWhat, Class<?> cl) {
		Intent intent = new Intent(context, cl);
		intent.putExtra(ConstantGloble.ALARMACTION, action);
		PendingIntent sender = null;
		switch (getWhat) {
		case 0:
			sender = PendingIntent.getActivity(context, requestCode, intent,
					PendingIntent.FLAG_CANCEL_CURRENT);
			break;
		case 1:
			sender = PendingIntent.getBroadcast(context, requestCode, intent,
					PendingIntent.FLAG_CANCEL_CURRENT);
			break;
		case 2:
			sender = PendingIntent.getService(context, requestCode, intent,
					PendingIntent.FLAG_CANCEL_CURRENT);
			break;
		default:
			break;
		}
		if (null == sender)
			return;
		AlarmManager am = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		am.cancel(sender);
	}

	/**
	 * @param context
	 * 
	 * @param flag
	 *            周期性时间间隔的标志,flag = 0 表示一次性的闹钟, flag = 1 表示每天提醒的闹钟(1天的时间间隔),flag
	 *            = 2 表示按周每周提醒的闹钟（一周的周期性时间间隔）
	 * @param hour
	 *            时
	 * @param minute
	 *            分
	 * @param requestCode
	 *            闹钟的id
	 * @param week
	 *            week=0表示一次性闹钟或者按天的周期性闹钟，非0 的情况下是几就代表以周为周期性的周几的闹钟 // * @param
	 *            soundOrVibrator // * 2表示声音和震动都执行，1表示只有铃声提醒，0表示只有震动提醒
	 * @param getWhat
	 *            0表示Activity，1表示Broadcast，2表示Service
	 * @param cl
	 *            三大组件之一
	 */

	public static void setAlarm(Context context, int flag, int hour,
			int minute, int requestCode, int week, int action, int getWhat,
			Class<?> cl) {
		AlarmManager am = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		Calendar calendar = Calendar.getInstance();
		long intervalMillis = 0;
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH), hour, minute, 0);
		if (flag == 0) {
			intervalMillis = 0;
		} else if (flag == 1) {
			intervalMillis = 24 * 3600 * 1000;
		} else if (flag == 2) {
			intervalMillis = 24 * 3600 * 1000 * 7;
		}
		Intent intent = new Intent(context, cl);
		intent.putExtra(ConstantGloble.ALARMACTION, action);
		intent.putExtra(ConstantGloble.INTERVAL_MILLIS, intervalMillis);
		intent.putExtra("COM_TYPE", getWhat);
		String hourStr = hour <10?("0"+hour):(""+hour);
		String minuteStr = minute <10?("0"+minute):(""+minute);
		intent.putExtra("ALARM_TIME", hourStr +":"+minuteStr);
		intent.putExtra(ConstantGloble.REQUEST_CODE, requestCode);
		PendingIntent sender = null;
		switch (getWhat) {
		case 0:
			sender = PendingIntent.getActivity(context, requestCode, intent,
					PendingIntent.FLAG_CANCEL_CURRENT);
			break;
		case 1:
			sender = PendingIntent.getBroadcast(context, requestCode, intent,
					PendingIntent.FLAG_CANCEL_CURRENT);
			break;
		case 2:
			sender = PendingIntent.getService(context, requestCode, intent,
					PendingIntent.FLAG_CANCEL_CURRENT);
			break;
		default:
			break;
		}
		if (null == sender)
			return;
//		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//			am.setWindow(AlarmManager.RTC_WAKEUP,
//					calMethod(week, calendar.getTimeInMillis()),
//					intervalMillis, sender);
//		} else {
			if (flag == 0) {
				am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
						sender);
		} else {
			am./* setRepeating */set(AlarmManager.RTC_WAKEUP,
					calMethod(week, calendar.getTimeInMillis()),
					AlarmManager.WINDOW_EXACT, intervalMillis, sender, null);
		}
//		}
	}

	/**
	 * @param weekflag
	 *            传入的是周几
	 * @param dateTime
	 *            传入的是时间戳（设置当天的年月日+从选择框拿来的时分秒）
	 * @return 返回起始闹钟时间的时间戳
	 */
	private static long calMethod(int weekflag, long dateTime) {
		long time = 0;
		// weekflag == 0表示是按天为周期性的时间间隔或者是一次行的，weekfalg非0时表示每周几的闹钟并以周为时间间隔
		if (weekflag != 0) {
			Calendar c = Calendar.getInstance();
			int week = c.get(Calendar.DAY_OF_WEEK);
			if (1 == week) {
				week = 7;
			} else if (2 == week) {
				week = 1;
			} else if (3 == week) {
				week = 2;
			} else if (4 == week) {
				week = 3;
			} else if (5 == week) {
				week = 4;
			} else if (6 == week) {
				week = 5;
			} else if (7 == week) {
				week = 6;
			}

			if (weekflag == week) {
				if (dateTime > System.currentTimeMillis()) {
					time = dateTime;
				} else {
					time = dateTime + 7 * 24 * 3600 * 1000;
				}
			} else if (weekflag > week) {
				time = dateTime + (weekflag - week) * 24 * 3600 * 1000;
			} else if (weekflag < week) {
				time = dateTime + (weekflag - week + 7) * 24 * 3600 * 1000;
			}
		} else {
			if (dateTime > System.currentTimeMillis()) {
				time = dateTime;
			} else {
				time = dateTime + 24 * 3600 * 1000;
			}
		}
		return time;
	}

}
